<?php
/**
 * gpa_new Description
 * @package
 * @name model_gallery
 * @filesource model_gallery.php
 * @author Meta Sanjaya
 */

class model_screening extends Model
{
    /**
     * @access public
     * @method __construct
     * @param void
     * @return void
     */
    public function  __construct()
    {
        parent::__construct();
        getInstance('config')->retrieve('screening');
    }

    public function getStepOneImages($start=0, $limit=30) {
        $uid = intval(@User::getUserId());
        $imgs = array();
        $step_point = json_decode(getInstance('config')->get('screening_step_point'), true);
        $point = intval($step_point["1"]);

        $images = $this->db->query("SELECT a.image_id FROM #_gallery_images a JOIN #_gallery_images_albums b ON a.image_id = b.image_id JOIN #_users_albums c ON b.album_id = c.album_id JOIN #_screening_images_point d ON d.image_id = a.image_id WHERE a.published='1' AND a.scope='screening' AND (a.image_id NOT IN (SELECT image_id FROM #_gallery_images_point WHERE user_id = '$uid' UNION DISTINCT SELECT image_id FROM #_gallery_images_albums WHERE album_id IN (SELECT album_id FROM #_users_albums WHERE user_id = '$uid' OR album_name = 'Not Selected'))) AND d.point < $point ORDER BY a.time_stamp DESC LIMIT $start, $limit")->rows;

        //var_dump($this->db->getQuery());
        //exit;

        $model_image = $this->getModel('gallery');

        for($i=0, $j=count($images); $i<$j; $i++) {
            $options['thumb_size'] = getInstance('config')->get('screening_thumb_size');
            $imgs[] = $model_image->getImageById($images[$i]['image_id'], $options);
        }

        return $imgs;
    }

    public function getImageFromId($iid) {
        $return = array();
        $uid = intval(@User::getUserId());
        $imgs = array();
        $step_point = json_decode(getInstance('config')->get('screening_step_point'), true);
        $point = intval($step_point["1"]);
        
        $images = $this->db->query("SELECT a.image_id FROM #_gallery_images a JOIN #_gallery_images_albums b ON a.image_id = b.image_id JOIN #_users_albums c ON b.album_id = c.album_id JOIN #_screening_images_point d ON d.image_id = a.image_id WHERE a.published='1' AND a.scope='screening' AND (a.image_id NOT IN (SELECT image_id FROM #_gallery_images_point WHERE user_id = '$uid' UNION DISTINCT SELECT image_id FROM #_gallery_images_albums WHERE album_id IN (SELECT album_id FROM #_users_albums WHERE user_id = '$uid' OR album_name = 'Not Selected'))) AND d.point < $point AND a.image_id = '$iid'")->rows;

        //var_dump($this->db->getQuery());
        //exit;
        
        $model_image = $this->getModel('gallery');

        for($i=0, $j=count($images); $i<$j; $i++) {
            $options['thumb_size'] = getInstance('config')->get('screening_thumb_size');
            $return[] = $model_image->getImageById($images[$i]['image_id'], $options);
        }

        return $return;
    }

    public function getStepTwoImages() {
        $return = array();
        $uid = intval(@User::getUserId());
        $step_point = json_decode(getInstance('config')->get('screening_step_point'), true);
        $point1 = intval($step_point["1"]);
        $point2 = intval($point1+$step_point["2"]-1);

        $images = $this->db->query("SELECT a.image_id FROM #_gallery_images a JOIN #_gallery_images_point b ON b.image_id = a.image_id JOIN #_screening_images_point c ON c.image_id = a.image_id WHERE b.user_id <> '$uid' AND c.point BETWEEN $point1 AND $point2 ORDER BY a.image_id DESC")->rows;

        //var_dump($this->db->getQuery());
        //exit;

        $model_image = $this->getModel('gallery');

        for($i=0, $j=count($images); $i<$j; $i++) {
            $options['thumb_size'] = getInstance('config')->get('screening_thumb_size');
            $return[] = $model_image->getImageById($images[$i]['image_id'], $options);
        }

        return $return;
    }

    public function pointImage($iid, $pointMarker, $comment, $step) {
        $session = User::getSession();
        $uid = $session['user_id'];
        $gid = strval($session['group_id']);
        $comment = $this->db->escape($comment);

        $rows = $this->db->query("SELECT point FROM #_screening_images_point WHERE image_id = '$iid'")->rows;
        if(count($rows)) {
            $point = intval($rows[0]['point']);
            $config = json_decode(getInstance('config')->get('screening_point_rules'), true);
            
            $newpoint = ($pointMarker=="+") ? $point+$config[$gid] : $point-$config[$gid];

            $this->db->query("UPDATE #_screening_images_point SET point = '$newpoint' WHERE image_id = '$iid'");
            $this->db->query("INSERT INTO #_gallery_images_point SET image_id = '$iid', user_id = '$uid', step='$step', comment = '$comment'");

            return true;
        }
    }
}
?>